package org.example.admin.utils;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        /**
         * 获取配置文件对应的输入流
         */
        try {
            InputStream is = Resources.getResourceAsStream("META-INF/applicationContext.xml");

            sqlSessionFactory = new MybatisSqlSessionFactoryBuilder().build(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取sqlSessionFactory
     *
     */
    public static SqlSession getSqlSession() {
        return sqlSessionFactory.openSession();
    }
}
